package com.mk.admin.util;

import java.util.*;
import java.math.BigDecimal;

public class UnitConverter {


        private static final Map<String, Map<String, BigDecimal>> UNIT_MAP = createUnitMap();

            public static BigDecimal getUnitScale(String from, String to, Object payload) {
            if (from == null || to == null) {
                return null;
            }
            if (from.equals(to)) {
                return BigDecimal.ONE;
            }

            Map<String, Map<String, BigDecimal>> unitMap = getMeta(payload);
            if (unitMap == null) {
                unitMap = UNIT_MAP;
            }

            if (unitMap.containsKey(from)) {
                return unitMap.get(from).get(to);
            }

            return null;
        }

        private static Map<String, Map<String, BigDecimal>> getMeta(Object payload) {
            // 这里可以根据payload获取自定义的单位映射
            // 示例中未实现payload处理逻辑
            return null;
        }

        private static Map<String, Map<String, BigDecimal>> createUnitMap() {
            Map<String, Map<String, BigDecimal>> unitMap = new HashMap<>();

            // kg 转换
            Map<String, BigDecimal> kgMap = new HashMap<>();
            kgMap.put("g", new BigDecimal("1000.0"));
            kgMap.put("mg", new BigDecimal("1000000.0"));
            kgMap.put("μg", new BigDecimal("1000000000.0"));
            unitMap.put("kg", kgMap);

            // g 转换
            Map<String, BigDecimal> gMap = new HashMap<>();
            gMap.put("kg", new BigDecimal("0.001"));
            gMap.put("mg", new BigDecimal("1000.0"));
            gMap.put("μg", new BigDecimal("1000000.0"));
            unitMap.put("g", gMap);

            // mg 转换
            Map<String, BigDecimal> mgMap = new HashMap<>();
            mgMap.put("kg", new BigDecimal("0.000001"));
            mgMap.put("g", new BigDecimal("0.001"));
            mgMap.put("μg", new BigDecimal("1000.0"));
            unitMap.put("mg", mgMap);

            // μg 转换
            Map<String, BigDecimal> μgMap = new HashMap<>();
            μgMap.put("kg", new BigDecimal("0.000000001"));
            μgMap.put("g", new BigDecimal("0.000001"));
            μgMap.put("mg", new BigDecimal("0.001"));
            unitMap.put("μg", μgMap);

            // l 转换
            Map<String, BigDecimal> lMap = new HashMap<>();
            lMap.put("ml", new BigDecimal("1000.0"));
            unitMap.put("l", lMap);

            // ml 转换
            Map<String, BigDecimal> mlMap = new HashMap<>();
            mlMap.put("l", new BigDecimal("0.001"));
            unitMap.put("ml", mlMap);

            // wu 转换
            Map<String, BigDecimal> wuMap = new HashMap<>();
            wuMap.put("U", new BigDecimal("10000"));
            wuMap.put("MU", new BigDecimal("0.001"));
            wuMap.put("万u", BigDecimal.ONE);
            wuMap.put("万U", BigDecimal.ONE);
            unitMap.put("wu", wuMap);

            // U 转换
            Map<String, BigDecimal> uMap = new HashMap<>();
            uMap.put("wu", new BigDecimal("0.00001"));
            uMap.put("万u", new BigDecimal("0.00001"));
            uMap.put("万U", new BigDecimal("0.00001"));
            uMap.put("MU", new BigDecimal("0.0000001"));
            unitMap.put("U", uMap);

            // MU 转换
            Map<String, BigDecimal> muMap = new HashMap<>();
            muMap.put("wu", new BigDecimal("100"));
            muMap.put("万u", new BigDecimal("100"));
            muMap.put("万U", new BigDecimal("100"));
            muMap.put("U", new BigDecimal("1000000"));
            unitMap.put("MU", muMap);

            // 万u 转换
            Map<String, BigDecimal> wanUMap = new HashMap<>();
            wanUMap.put("U", new BigDecimal("10000"));
            wanUMap.put("MU", new BigDecimal("0.001"));
            wanUMap.put("万u", BigDecimal.ONE);
            wanUMap.put("万U", BigDecimal.ONE);
            unitMap.put("万u", wanUMap);

            // 万U 转换
            Map<String, BigDecimal> wanUMap2 = new HashMap<>();
            wanUMap2.put("U", new BigDecimal("10000"));
            wanUMap2.put("MU", new BigDecimal("0.001"));
            wanUMap2.put("万u", BigDecimal.ONE);
            wanUMap2.put("万U", BigDecimal.ONE);
            unitMap.put("万U", wanUMap2);

            // L/24h 转换
            Map<String, BigDecimal> l24hMap = new HashMap<>();
            l24hMap.put("ml/min", new BigDecimal("0.6944"));
            unitMap.put("L/24h", l24hMap);

            return unitMap;
        }
}
